package martian.t1me
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import martian.t1me.core.Sequence;
	import martian.t1me.core.Sequencable;
	
	public class Call extends EventDispatcher implements Sequencable
	{
		private var func:Function;
		private var timer:Timer;
		
		public function Call(func:Function, delay:Number = 0):void
		{
			this.func = func;
			timer = new Timer(delay * 1000, 1);
		}
		
		public function start():void
		{
			dispatchEvent(new Event(Sequence.START));
			
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, stop);
			timer.start();
		}
		
		private function stop(e:TimerEvent):void
		{
			if (func != null) { func.call(); }
		
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, stop);
			timer.reset();
			
			dispatchEvent(new Event(Sequence.STOP));
		}
	}
}